from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ImproperlyConfigured

class ActionRegistry:
    def __init__(self):
        self.actions = {}

    def register(self, model_name, name, func=None):
        if func is None:
            return lambda f: self.register(model_name, name, f)
        
        if model_name not in self.actions:
            self.actions[model_name] = {}
        
        self.actions[model_name][name] = func
        return func

    def get_action(self, model_name, name):
        model_actions = self.actions.get(model_name, {})
        return model_actions.get(name)

    def get_actions(self, model_name):
        return self.actions.get(model_name, {})

action_registry = ActionRegistry()

def register_action(model_name, name):
    return action_registry.register(model_name, name)

class ModelActionMixin:
    def get_actions(self):
        if not hasattr(self, 'model'):
            raise ImproperlyConfigured(
                "%(cls)s is missing a model. Define "
                "%(cls)s.model or override %(cls)s.get_actions()." % {
                    'cls': self.__class__.__name__
                }
            )
        return action_registry.get_actions(self.model.__name__)

    def process_action(self, request, queryset):
        action = request.POST.get('action')
        if action:
            action_func = action_registry.get_action(self.model.__name__, action)
            if action_func:
                return action_func(request, queryset)
        return None